$(function (){
    //从缓存中获取数据（在登录成功后会将当前用户和用户令牌放入缓存中）
    const user = JSON.parse(window.sessionStorage.getItem("user"));
    const token = window.sessionStorage.getItem("token");
    if(user == null || token == null){
        alert("请先登录");
        window.location.href = "../html/welcome.html";
    }

    //导航球 下拉
    $("#gooey").gooeymenu({
        bgColor: "rgba(79,117,167,0.4)",
        contentColor: "white",
        style: "vertical",
        horizontal: {
            menuItemPosition: "glue"
        },
        vertical: {
            menuItemPosition: "spaced",
            direction: "down"
        },
        circle: {
            radius: 90
        },
        margin: "small",
        size: 70,
        bounce: true,
        bounceLength: "small",
        transitionStep: 100,
        hover: "rgba(79, 117, 167, 0.6)"
    });

    //滚动条
    $(".tucao-content").scrollBar({
        barWidth: 5, //滚动条的宽度(这里根据需要写数值即可，不设置是10,即默认10px)
        position: "y", //写“x”代表只出水平滚动条，写“y”表示只出垂直滚动条，写“x,y”则出水平和垂直滚动条（只有在内容超出容器时才出现滚动条）
        wheelDis: 60 //滚轮滚动一次向下或向上滚动的距离，默认是15，可根据需要修改数值
    });

    //初始化心灵鸡汤分页展示 底部分页框初始化
    heartPageAjax(token,1,user.userId);

    //给发布心灵鸡汤按钮添加点击事件
    $("#addHeartBtn").click(function (){
        const username = user.username;
        const content = $("#heart-content").val();
        const likeNum = 0;
        const discussNum = 0;
        const isDelete = 1;
        const place = returnCitySN.cname;
        const verify = 0;

        const heartData = new HeartData(username,content,likeNum,discussNum,isDelete,place,verify);

        addHeartAjax(token,heartData);
    });

    //添加点击跳转评论界面功能
    for (let i = 0; i < 10; i++) {
        $("#heartContent" + i).click(function (){
            location.href = "discuss.html?username=" + $("#username" + i).text() + "&time=" + $("#time" + i).text() +
                "&place=" + $("#place" + i).text() + "&heartContent=" + $("#heartContent" + i).text() +
                "&heartId=" + $("#heartId" + i).text() +"&tucaoOrHeart=heart";
        });
    }

    //添加点赞点击事件
    for (let i = 0; i < 10; i++) {
        $("#likeBtn" + i).click(function (){
            const userId = user.userId;
            const heartId = $("#heartId" + i).text();
            const likeCronClass = $("#likeCron" + i).attr("class");
            //点赞按钮是空心的 未点赞
            if(likeCronClass == "fa fa-heart-o"){
                const like = new Like(userId,heartId,1);
                likeHeart(token,like,i,userId);
            }
            //点赞按钮是实心的 已点赞
            else if(likeCronClass == "fa fa-heart"){
                cancelLikeHeart(token,userId,heartId);
            }
        });
    }

    //添加搜索按钮点击事件
    $("#searchHeartBtn").click(function (){
        const heartFindSort = $("#heart-find-sort option:selected").text();
        const heartFindContent = $("#heart-find-content").val();
        if (heartFindSort == "按鸡汤内容查找"){
            searchHeartPageAjax(token,1,heartFindContent,user.userId);
        }else if(heartFindSort == "按发布鸡汤用户名查找") {
            searchHeartPageByUserAjax(token,1,heartFindContent,user.userId);
        }
    });

    //添加排序按钮点击事件
    $("#sortHeartBtn").click(function (){
        const heartFindSort = $("#heart-sort-by option:selected").text();
        sortHeartPageAjax(token,1,heartFindSort,user.userId);
    });

    //关闭页面时调用logout方法，退出当前登录用户
    $(window).unload(function(){
        $.ajax({
            type:"POST",
            contentType:"application/json",
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            url :"http://localhost:2082/logout?username=" + user.username
        })
    });
});


//分页获取排序后的鸡汤数据ajax
function sortHeartPageAjax(token,current,sortBy,userId){
    $.ajax({
        type: "GET",
        contentType: "application/json",
        headers: {
            "token":token
        },
        url: "http://localhost:2082/heart/sort?current=" + current +"&sortBy=" + sortBy,
        success: function(data,status,xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const responseToken = xhr.getResponseHeader("token");
            if(responseToken == "overdue"){
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            }else {
                let result = JSON.stringify(data);
                result = eval("("+result+")");
                //操作成功
                if(result.code == 200){
                    $('#sortHeart').modal('hide');
                    const pageData = result.data;
                    //records：获取到的分页数组数据
                    const records = eval("("+JSON.stringify(pageData)+")").records;
                    //total：所有数据的数量
                    const total = eval("("+JSON.stringify(pageData)+")").total;
                    //size：每页数据的数量
                    const size = eval("("+JSON.stringify(pageData)+")").size;
                    //current：当前页数
                    const current = eval("("+JSON.stringify(pageData)+")").current;
                    //总页数
                    const pageNum = eval("("+JSON.stringify(pageData)+")").pages;

                    //先清空数据
                    for (let i = 0; i < 10; i++) {
                        $("#username" + i).empty();
                        $("#time" + i).empty();
                        $("#place" + i).empty();
                        $("#heartContent" + i).empty();
                        $("#likeNum" + i).empty();
                        $("#discussNum" + i).empty();
                    }
                    //再进行赋值
                    let heartNum;
                    if( (total-size*(current-1)) >= 10){
                        heartNum = 10;
                    }else if( (total-size*(current-1)) < 10){
                        heartNum = total-size*(current-1);
                        for (let i = heartNum; i < 10; i++) {
                            //添加不可见样式
                            $("#heart" + i).attr("style","display:none");
                        }
                    }
                    for (let i = 0; i < heartNum; i++) {
                        //移除不可见样式
                        $("#heart" + i).removeAttr("style");
                        const username = JSON.stringify(records[i].username);
                        const time = JSON.stringify(records[i].createTime);
                        const place = JSON.stringify(records[i].place);
                        const heartContent = JSON.stringify(records[i].content);
                        const likeNum = JSON.stringify(records[i].likeNum);
                        const discussNum = JSON.stringify(records[i].discussNum);
                        const heartId = JSON.stringify(records[i].id);
                        $("#username" + i).html(username.replace(/^\"|\"$/g,''));
                        $("#time" + i).html(renderTime(time.replace(/^\"|\"$/g,'')));
                        $("#place" + i).html(place.replace(/^\"|\"$/g,''));
                        $("#heartContent" + i).html(heartContent.replace(/^\"|\"$/g,''));
                        $("#likeNum" + i).html(likeNum.replace(/^\"|\"$/g,''));
                        $("#discussNum" + i).html(discussNum.replace(/^\"|\"$/g,''));
                        $("#heartId" + i).html(heartId.replace(/^\"|\"$/g,''));

                        judgeIsLike(token,userId,heartId,i);
                    }

                    //初始化底部搜索栏
                    $("#pagination").pagination({
                        currentPage: current,
                        totalPage: pageNum,
                        isShow: true,
                        count: 10,
                        homePageText: "首页",
                        endPageText: "尾页",
                        prevPageText: "上一页",
                        nextPageText: "下一页",
                        callback: function(current) {
                            sortHeartPageAjax(token,current,sortBy,userId);
                        }
                    });
                }
                //操作失败
                else if(result.code == 500) {
                    spop({
                        template: result.msg,
                        position: 'top-center',
                        group: "submit-satus",
                        style: "fail",
                        autoclose: 2000
                    });
                }
            }
        }
    })
}

/**
 * 分页获取通过内容查询得到的鸡汤数据ajax
 * @param token 令牌
 * @param current 页码
 * @param heartContent 鸡汤搜索内容
 * @param userId 用户id
 */
function searchHeartPageAjax(token,current,heartContent,userId){
    $.ajax({
        type: "GET",
        contentType: "application/json",
        headers: {
            "token":token
        },
        url: "http://localhost:2082/heart/search-by-content?current=" + current + "&heartContent=" + heartContent,
        success: function(data,status,xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const responseToken = xhr.getResponseHeader("token");
            if(responseToken == "overdue"){
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            }else {
                let result = JSON.stringify(data);
                result = eval("("+result+")");
                //操作成功
                if(result.code == 200){
                    $("#searchHeart").modal('hide');
                    const pageData = result.data;
                    //records：获取到的分页数组数据
                    const records = eval("("+JSON.stringify(pageData)+")").records;
                    //total：所有数据的数量
                    const total = eval("("+JSON.stringify(pageData)+")").total;
                    //size：每页数据的数量
                    const size = eval("("+JSON.stringify(pageData)+")").size;
                    //current：当前页数
                    const current = eval("("+JSON.stringify(pageData)+")").current;
                    //总页数
                    const pageNum = eval("("+JSON.stringify(pageData)+")").pages;

                    //先清空数据
                    for (let i = 0; i < 10; i++) {
                        $("#username" + i).empty();
                        $("#time" + i).empty();
                        $("#place" + i).empty();
                        $("#heartContent" + i).empty();
                        $("#likeNum" + i).empty();
                        $("#discussNum" + i).empty();
                    }
                    //再进行赋值
                    let heartNum;
                    if( (total-size*(current-1)) >= 10){
                        heartNum = 10;
                    }else if( (total-size*(current-1)) < 10){
                        heartNum = total-size*(current-1);
                        for (let i = heartNum; i < 10; i++) {
                            //添加不可见样式
                            $("#heart" + i).attr("style","display:none");
                        }
                    }
                    for (let i = 0; i < heartNum; i++) {
                        //移除不可见样式
                        $("#heart" + i).removeAttr("style");
                        const username = JSON.stringify(records[i].username);
                        const time = JSON.stringify(records[i].createTime);
                        const place = JSON.stringify(records[i].place);
                        const heartContent = JSON.stringify(records[i].content);
                        const likeNum = JSON.stringify(records[i].likeNum);
                        const discussNum = JSON.stringify(records[i].discussNum);
                        const heartId = JSON.stringify(records[i].id);
                        $("#username" + i).html(username.replace(/^\"|\"$/g,''));
                        $("#time" + i).html(renderTime(time.replace(/^\"|\"$/g,'')));
                        $("#place" + i).html(place.replace(/^\"|\"$/g,''));
                        $("#heartContent" + i).html(heartContent.replace(/^\"|\"$/g,''));
                        $("#likeNum" + i).html(likeNum.replace(/^\"|\"$/g,''));
                        $("#discussNum" + i).html(discussNum.replace(/^\"|\"$/g,''));
                        $("#heartId" + i).html(heartId.replace(/^\"|\"$/g,''));

                        judgeIsLike(token,userId,heartId,i);
                    }

                    //初始化底部搜索栏
                    $("#pagination").pagination({
                        currentPage: current,
                        totalPage: pageNum,
                        isShow: true,
                        count: 10,
                        homePageText: "首页",
                        endPageText: "尾页",
                        prevPageText: "上一页",
                        nextPageText: "下一页",
                        callback: function(current) {
                            searchHeartPageAjax(token,current,heartContent,userId);
                        }
                    });
                }
                //操作失败
                else if(result.code == 500) {
                    spop({
                        template: result.msg,
                        position: 'top-center',
                        group: "submit-satus",
                        style: "fail",
                        autoclose: 2000
                    });
                }
            }
        }
    })
}

/**
 * 分页获取通过用户名查询得到的鸡汤数据ajax
 * @param token 令牌
 * @param current 页码
 * @param username 鸡汤搜索内容
 * @param userId 用户id
 */
function searchHeartPageByUserAjax(token,current,username,userId){
    $.ajax({
        type: "GET",
        contentType: "application/json",
        headers: {
            "token":token
        },
        url: "http://localhost:2082/heart/search-by-username?current=" + current + "&username=" + username,
        success: function(data,status,xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const responseToken = xhr.getResponseHeader("token");
            if(responseToken == "overdue"){
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            }else {
                let result = JSON.stringify(data);
                result = eval("("+result+")");
                //操作成功
                if(result.code == 200){
                    $('#searchHeart').modal('hide');
                    const pageData = result.data;
                    //records：获取到的分页数组数据
                    const records = eval("("+JSON.stringify(pageData)+")").records;
                    //total：所有数据的数量
                    const total = eval("("+JSON.stringify(pageData)+")").total;
                    //size：每页数据的数量
                    const size = eval("("+JSON.stringify(pageData)+")").size;
                    //current：当前页数
                    const current = eval("("+JSON.stringify(pageData)+")").current;
                    //总页数
                    const pageNum = eval("("+JSON.stringify(pageData)+")").pages;

                    //先清空数据
                    for (let i = 0; i < 10; i++) {
                        $("#username" + i).empty();
                        $("#time" + i).empty();
                        $("#place" + i).empty();
                        $("#heartContent" + i).empty();
                        $("#likeNum" + i).empty();
                        $("#discussNum" + i).empty();
                    }
                    //再进行赋值
                    let heartNum;
                    if( (total-size*(current-1)) >= 10){
                        heartNum = 10;
                    }else if( (total-size*(current-1)) < 10){
                        heartNum = total-size*(current-1);
                        for (let i = heartNum; i < 10; i++) {
                            //添加不可见样式
                            $("#heart" + i).attr("style","display:none");
                        }
                    }
                    for (let i = 0; i < heartNum; i++) {
                        //移除不可见样式
                        $("#heart" + i).removeAttr("style");
                        const username = JSON.stringify(records[i].username);
                        const time = JSON.stringify(records[i].createTime);
                        const place = JSON.stringify(records[i].place);
                        const heartContent = JSON.stringify(records[i].content);
                        const likeNum = JSON.stringify(records[i].likeNum);
                        const discussNum = JSON.stringify(records[i].discussNum);
                        const heartId = JSON.stringify(records[i].id);
                        $("#username" + i).html(username.replace(/^\"|\"$/g,''));
                        $("#time" + i).html(renderTime(time.replace(/^\"|\"$/g,'')));
                        $("#place" + i).html(place.replace(/^\"|\"$/g,''));
                        $("#heartContent" + i).html(heartContent.replace(/^\"|\"$/g,''));
                        $("#likeNum" + i).html(likeNum.replace(/^\"|\"$/g,''));
                        $("#discussNum" + i).html(discussNum.replace(/^\"|\"$/g,''));
                        $("#heartId" + i).html(heartId.replace(/^\"|\"$/g,''));

                        judgeIsLike(token,userId,heartId,i);
                    }

                    //初始化底部搜索栏
                    $("#pagination").pagination({
                        currentPage: current,
                        totalPage: pageNum,
                        isShow: true,
                        count: 10,
                        homePageText: "首页",
                        endPageText: "尾页",
                        prevPageText: "上一页",
                        nextPageText: "下一页",
                        callback: function(current) {
                            searchHeartPageByUserAjax(token,current,username,userId);
                        }
                    });
                }
                //操作失败
                else if(result.code == 500) {
                    spop({
                        template: result.msg,
                        position: 'top-center',
                        group: "submit-satus",
                        style: "fail",
                        autoclose: 2000
                    });
                }
            }
        }
    })
}

/**
 * 当前页码
 */
let lsqCurrentPage = 1;

/**
 * 点赞实体类
 * @param userId 用户id
 * @param heartId 用户点赞的鸡汤id
 * @param sort 分类；0->吐槽；1->鸡汤
 */
function Like(userId,heartId,sort){
    this.userId = userId;
    this.heartId = heartId;
    this.sort = sort;
}

/**
 * 点赞鸡汤 ajax
 * @param token 令牌
 * @param like 点赞实体类
 * @param i 点赞的鸡汤数据的排序（例如：点赞页面中展示的第一条鸡汤数据，i=0）
 */
function likeHeart(token,like,i,userId){
    $.ajax({
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify(like),
        headers: {
            "token":token
        },
        url: "http://localhost:2082/user-like/like",
        success: function(data,status,xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const responseToken = xhr.getResponseHeader("token");
            if(responseToken == "overdue"){
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            }else {
                let result = JSON.stringify(data);
                result = eval("("+result+")");
                //操作成功
                if(result.code == 200){
                    // alert(lsqCurrentPage);
                    //再次进行分页操作
                    heartPageAjax(token,lsqCurrentPage,userId);
                }
                //操作失败
                else if(result.code == 500){
                    spop({
                        template: result.msg,
                        position  : 'top-center',
                        group: "submit-satus",
                        style: "fail",
                        autoclose: 2000
                    });
                }
            }
        }
    });
}

/**
 * 取消对某条鸡汤的点赞
 * @param token 令牌
 * @param userId 用户id
 * @param tucaoOrHeartId 吐槽或鸡汤id
 */
function cancelLikeHeart(token,userId,tucaoOrHeartId){
    $.ajax({
        type: "POST",
        contentType: "application/json",
        headers: {
            "token":token
        },
        url: "http://localhost:2082/user-like/cancel-like?userId=" + userId +"&tucaoOrHeartId=" + tucaoOrHeartId + "&sort=1",
        success: function(data,status,xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const responseToken = xhr.getResponseHeader("token");
            if(responseToken == "overdue"){
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            }else {
                let result = JSON.stringify(data);
                result = eval("("+result+")");
                //操作成功
                if(result.code == 200){
                    //再次进行分页操作
                    heartPageAjax(token,lsqCurrentPage,userId);
                }
                //操作失败
                else if(result.code == 500){
                    spop({
                        template: result.msg,
                        position  : 'top-center',
                        group: "submit-satus",
                        style: "fail",
                        autoclose: 2000
                    });
                }
            }
        }
    });
}

/**
 * 通过userId和heartId，判断当前用户是否对这条鸡汤进行过点赞操作
 * @param token 令牌
 * @param userId 用户id
 * @param heartId 鸡汤id
 * @param i 当前进行判断的鸡汤数据的排序（例如：点赞页面中展示的第一条鸡汤数据，i=0）
 */
function judgeIsLike(token,userId,heartId,i){
    $.ajax({
        type: "GET",
        contentType: "application/json",
        headers: {
            "token":token
        },
        url: "http://localhost:2082/user-like/judge?userId=" + userId + "&tucaoOrHeartId=" + heartId + "&sort=1",
        success: function(data,status,xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const responseToken = xhr.getResponseHeader("token");
            if(responseToken == "overdue"){
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            }else {
                let result = JSON.stringify(data);
                result = eval("("+result+")");
                //表示该鸡汤已被当前用户点赞
                if(result.code == 200){
                    //先移除class
                    $("#likeCron"+i).removeAttr("class");
                    //再添加class 实心
                    $("#likeCron"+i).attr("class","fa fa-heart");
                }
                //未点赞
                else if(result.code == 500){
                    //先移除class
                    $("#likeCron"+i).removeAttr("class");
                    //再添加class 空心
                    $("#likeCron"+i).attr("class","fa fa-heart-o");
                }
            }
        }
    });
}

/**
 * 心灵鸡汤实体类
 * @param username 用户名
 * @param content 鸡汤内容
 * @param likeNum 点赞数
 * @param discussNum 点赞数
 * @param isDelete 是否删除；0->已删除；1->未删除
 * @param place 发表鸡汤的地点
 * @param verify 是否通过审核；0->为审核；1->审核通过；2->审核未通过
 * @constructor
 */
function HeartData(username,content,likeNum,discussNum,isDelete,place,verify) {
    this.username = username;
    this.content = content;
    this.likeNum = likeNum;
    this.discussNum = discussNum;
    this.isDelete = isDelete;
    this.place = place;
    this.verify = verify;
}

/**
 * 添加心灵鸡汤ajax
 * @param token 令牌
 * @param heartData 发表的心灵鸡汤实体类
 */
function addHeartAjax(token,heartData){
    $.ajax({
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify(heartData),
        headers: {
            "token":token
        },
        url: "http://localhost:2082/heart",
        success: function(data,status,xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const token = xhr.getResponseHeader("token");
            if(token == "overdue"){
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            }else {
                let result = JSON.stringify(data);
                result = eval("("+result+")");
                //操作成功
                if(result.code == 200){
                    $('#addHeart').modal('hide');
                    spop({
                        template: result.msg,
                        position  : 'top-center',
                        group: "submit-satus",
                        style: "success",
                        autoclose: 2000
                    });
                }
                //操作失败
                else if(result.code == 500){
                    spop({
                        template: result.msg,
                        position  : 'top-center',
                        group: "submit-satus",
                        style: "fail",
                        autoclose: 2000
                    });
                }
            }
        }
    })
}

/**
 * 分页获取心灵鸡汤数据ajax
 * @param token 令牌
 * @param current 页码
 */
function heartPageAjax(token,current,userId){
    $.ajax({
        type: "GET",
        contentType: "application/json",
        headers: {
            "token":token
        },
        url: "http://localhost:2082/heart/page?verify=1&current=" + current,
        success: function(data,status,xhr) {
            //若用户认证过期，后端返回的响应头中会有一个token字段，值为overdue
            const responseToken = xhr.getResponseHeader("token");
            if(responseToken == "overdue"){
                alert("用户认证已过期，请重新登录");
                window.location.href = "../html/welcome.html";
            }else {
                let result = JSON.stringify(data);
                result = eval("("+result+")");
                //操作成功
                if(result.code == 200){
                    const pageData = result.data;
                    //records：获取到的分页数组数据
                    const records = eval("("+JSON.stringify(pageData)+")").records;
                    //total：所有数据的数量
                    const total = eval("("+JSON.stringify(pageData)+")").total;
                    //size：每页数据的数量
                    const size = eval("("+JSON.stringify(pageData)+")").size;
                    //current：当前页数
                    const current = eval("("+JSON.stringify(pageData)+")").current;
                    //总页数
                    const pageNum = eval("("+JSON.stringify(pageData)+")").pages;

                    //先清空数据
                    for (let i = 0; i < 10; i++) {
                        $("#username" + i).empty();
                        $("#time" + i).empty();
                        $("#place" + i).empty();
                        $("#heartContent" + i).empty();
                        $("#likeNum" + i).empty();
                        $("#discussNum" + i).empty();
                    }
                    //再进行赋值
                    let heartNum;
                    if( (total-size*(current-1)) >= 10){
                        heartNum = 10;
                    }else if( (total-size*(current-1)) < 10){
                        heartNum = total-size*(current-1);
                        for (let i = heartNum; i < 10; i++) {
                            //添加不可见样式
                            $("#heart" + i).attr("style","display:none");
                        }
                    }
                    for (let i = 0; i < heartNum; i++) {
                        //移除不可见样式
                        $("#heart" + i).removeAttr("style");
                        const username = JSON.stringify(records[i].username);
                        const time = JSON.stringify(records[i].createTime);
                        const place = JSON.stringify(records[i].place);
                        const heartContent = JSON.stringify(records[i].content);
                        const likeNum = JSON.stringify(records[i].likeNum);
                        const discussNum = JSON.stringify(records[i].discussNum);
                        const heartId = JSON.stringify(records[i].id);
                        $("#username" + i).html(username.replace(/^\"|\"$/g,''));
                        $("#time" + i).html(renderTime(time.replace(/^\"|\"$/g,'')));
                        $("#place" + i).html(place.replace(/^\"|\"$/g,''));
                        $("#heartContent" + i).html(heartContent.replace(/^\"|\"$/g,''));
                        $("#likeNum" + i).html(likeNum.replace(/^\"|\"$/g,''));
                        $("#discussNum" + i).html(discussNum.replace(/^\"|\"$/g,''));
                        $("#heartId" + i).html(heartId.replace(/^\"|\"$/g,''));
                        //获取当前用户是否对该鸡汤进行过点赞操作
                        judgeIsLike(token,userId,heartId,i);
                    }

                    //初始化底部搜索栏
                    $("#pagination").pagination({
                        currentPage: current,
                        totalPage: pageNum,
                        isShow: true,
                        count: 10,
                        homePageText: "首页",
                        endPageText: "尾页",
                        prevPageText: "上一页",
                        nextPageText: "下一页",
                        callback: function(current) {
                            lsqCurrentPage = current;
                            heartPageAjax(token,current,userId);
                        }
                    });
                }
                //操作失败
                else if(result.code == 500){
                    spop({
                        template: result.msg,
                        position  : 'top-center',
                        group: "submit-satus",
                        style: "fail",
                        autoclose: 2000
                    });
                }

            }
        }
    });
}

/**
 * 时间格式转换
 * @param date
 * @returns {string}
 */
function renderTime(date) {
    var dateee = new Date(date).toJSON();
    // return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
    return new Date(+new Date(dateee)).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
}

/**
 * 背景 森林背景
 */
function run() {
    var image = document.getElementById('background');
    image.onload = function() {
        var engine = new RainyDay({
            image: this,
            blur: 10,
            opacity: 1
        });
        engine.rain([
            [1, 2, 15000]
        ]);
        engine.rain([
            [3, 3, 0.88],
            [5, 5, 0.9],
            [6, 2, 1]
        ], 100);
    };
    image.crossOrigin = 'anonymous';
    image.src = '../img/bg2.jpg';
}


